<template>
  <div class="myBox">
    <div class="centerBox">
      <div class="left_box">
        <div class="left_form">
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginFormrules"
            status-icon
            @keydown.enter.native="login"
          >
            <el-form-item>
              <span class="mySpan1">用户登录</span>
            </el-form-item>

            <el-form-item prop="uName">
              <el-input
                v-model="loginForm.uName"
                placeholder="请输入用户名"
                prefix-icon="el-icon-user-solid"
              ></el-input>
            </el-form-item>

            <el-form-item prop="uPass">
              <el-input
                v-model="loginForm.uPass"
                placeholder="请输入密码"
                prefix-icon="el-icon-lock"
                show-password
                type="password"
              ></el-input>
            </el-form-item>

            <el-form-item>
              <el-radio v-model="radio" label="1">医生</el-radio>
              <el-radio v-model="radio" label="2">用户</el-radio>
            </el-form-item>

            <el-form-item>
              <Vcode :show="isShow" @success="success" @close="close" />
              <el-button @click="login" @keydown.enter="login">登录</el-button>
            </el-form-item>
          </el-form>

          <span class="mySpan2"
            >还没有账号？？<u @click="toRegister">立即注册</u></span
          >
        </div>
      </div>

      <div class="right_box">
        <img src="../assets/images/login.png" class="myImg" />
      </div>
    </div>
  </div>
</template>

<script>
import Vcode from "vue-puzzle-vcode";
import axios from "axios";

export default {
  name: "login",
  comName: "登录",

  data() {
    return {
      loginForm: {
        uName: "",
        uPass: "",
      },
      radio: "2",
      isShow: false,
      loginFormrules: {
        uName: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
        uPass: [{ required: true, message: "密码不能为空", trigger: "blur" }],
      },
    };
  },
  components: {
    Vcode,
  },

  methods: {
    toRegister() {
      this.$router.push({ path: "/register" });
    },

    login() {
      if (this.loginForm.uName !== "" || this.loginForm.uPass !== "") {
        if (this.radio !== "") {
          this.isShow = true;
        } else {
          this.$message.error("登录失败，请选择身份");
        }
      } else {
        this.$message.error("登录失败，请将用户名和密码填写完整");
      }
    },
    // 用户通过了验证
    success() {
      this.isShow = false; // 通过验证后，需要手动隐藏模态框
        
      if (this.radio === '2') {
        axios({
          url: "/api/user/login",
          method: "post",
          data: {
            ...this.loginForm,
          },
        }).then(res => {
          // console.log("ididididididid"+res.data.list[0].uId);
          if (res.data.error === 0) {
            this.$notify({
              title: "登录成功",
              message: "登录成功，正在跳转页面",
              type: "success",
            });
            sessionStorage.setItem("Token", res.data.token);
            sessionStorage.setItem("uId",res.data.list[0].uId);
            sessionStorage.setItem("uName",res.data.list[0].uName);
            setTimeout(() => {
              if(res.data.list[0].roleId === 0){
                 this.$router.push({ 
                   path: "/user/home",
                   query:{
                     uId:res.data.list[0].uId
                   }
                  });
              }else if(res.data.list[0].roleId === 1 ){
                    this.$router.push({ path: "/System/doctor" });
              }
            }, 1000);
          } else {
            this.$notify.error({
              title: "登录失败",
              message: res.data.info,
            });
          }
        });
      }else if (this.radio === '1') {
          axios({
          url: "/api/doctor/login",
          method: "post",
          data: {
            doctorName:this.loginForm.uName,
            doctorPass:this.loginForm.uPass,
          },
        }).then(res => {
          console.log(res);
          if (res.data.error === 0) {
            this.$notify({
              title: "登录成功",
              message: "登录成功，正在跳转页面",
              type: "success",
            });
            sessionStorage.setItem("Token", res.data.token);
             sessionStorage.setItem("doctorId",res.data.list[0].doctorId);
             sessionStorage.setItem("doctorName",res.data.list[0].doctorName);
            setTimeout(() => {
                 this.$router.push({ path: "/doctor/Search" });
            }, 1000);
          } else {
            this.$notify.error({
              title: "登录失败",
              message: res.data.info,
            });
          }
        });
      }
    },
    // 用户点击遮罩层，应该关闭模态框
    close() {
      this.isShow = false;
    },
  },

  mounted() {},
};
</script>


<style lang="scss" scoped>
.myBox {
  background-color: #f8f9fb;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;

  .centerBox {
    background-color: rgb(255, 255, 255);
    width: 80%;
    height: 600px;
    box-shadow: 0px 0px 8px rgb(160, 160, 160);
    border-radius: 15px;
    display: flex;

    .left_box {
      background-color: rgb(255, 255, 255);
      border-radius: 15px 0px 0px 15px;
      height: 600px;
      width: 65%;
      display: flex;
      justify-content: center;
      align-items: center;

      .mySpan2 {
        font-size: 3px;
        color: rgb(90, 90, 90);

        u {
          font-size: 15px;
          font-weight: bold;
          color: #5a85fd;
        }
      }

      .left_form {
        width: 65%;
        text-align: center;

        .mySpan1 {
          font-size: 28px;
          font-weight: bold;
          letter-spacing: 15px;
        }

        .el-button {
          width: 100%;
          height: 40px;
          background-color: #64b9ff;
          color: #fff;
          letter-spacing: 30px;
          font-size: 16px;
        }
        .el-input /deep/ .el-input__inner {
          background-color: #f8f9fb;
        }
      }
    }

    .right_box {
      background-color: #d0edff;
      border-radius: 0px 15px 15px 0px;
      height: 600px;
      width: 90%;
      display: flex;
      justify-content: center;
      align-items: center;

      .myImg {
        width: 100%;
        height: 400px;
      }
    }
  }
}
</style>
